package com.zhkj.txg.module.home.adapter

import android.graphics.Paint
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.home.entity.SearchProductEntity
import com.zhkj.txg.utils.ImageUtils

class CateProductAdapter :
    BaseQuickAdapter<SearchProductEntity, BaseViewHolder>(R.layout.item_cate_product),
    LoadMoreModule {

    private val isMember = AccountManager.isMember()

    override fun convert(helper: BaseViewHolder, item: SearchProductEntity) {

        //是否活动价
        val isActivePrice: Boolean = !item.activePrice.isNullOrEmpty()
        //是否会员价
        val isPlusPrice: Boolean = !item.plusPrice.isNullOrEmpty() && item.activePrice.isNullOrEmpty()

        val tvGoodsPrice: TextView = helper.getView(R.id.tvCateProductPrice)
        val tvGoodsPrice2: TextView = helper.getView(R.id.tvCateProductPrice2)
        val llGoodsPush: LinearLayout = helper.getView(R.id.llCateProductPush)
        val tvGoodsPushPrice: TextView = helper.getView(R.id.tvCateProductPushPrice)
        val tvGoodsDiscount: TextView = helper.getView(R.id.tvCateProductDiscount)

        if (isActivePrice) {
            tvGoodsPrice.visibility = View.VISIBLE
            tvGoodsPrice2.visibility = View.VISIBLE
            tvGoodsDiscount.visibility = View.VISIBLE
            llGoodsPush.visibility = View.GONE
            tvGoodsPrice.text = SpanUtil.setMoneySymbol(item.activePrice, true)
            tvGoodsPrice2.text = SpanUtil.setMoneySymbol(item.price, false)
            tvGoodsPrice2.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG
            tvGoodsDiscount.text = "${item.discount}折"
        } else if (isPlusPrice) {
            tvGoodsPrice.visibility = View.VISIBLE
            tvGoodsPrice2.visibility = View.GONE
            tvGoodsDiscount.visibility = View.GONE
            llGoodsPush.visibility = View.VISIBLE
            tvGoodsPrice.text = SpanUtil.setMoneySymbol(item.price, false)
            tvGoodsPushPrice.text = SpanUtil.setMoneySymbol(item.plusPrice, false)
        } else {
            tvGoodsPrice.visibility = View.VISIBLE
            tvGoodsPrice2.visibility = View.GONE
            tvGoodsDiscount.visibility = View.GONE
            llGoodsPush.visibility = View.GONE
            tvGoodsPrice.text = SpanUtil.setMoneySymbol(item.price, false)
        }


        val imageView = helper.setText(R.id.tvCateProductName, item.goodsName)
//            .setText(R.id.tvCateProductPrice, SpanUtil.setMoneySymbol(item.price, true))
//            .setText(R.id.tvProductSale, context.getString(R.string.sale_format, item.saleCount ?: "0"))
//            .setGone(R.id.tvCateProductTag, !TextUtils.equals(item.isNew, "1"))
//            .setText(
//                R.id.tvCateProductShopName,
//                if (item.shopName.isEmpty()) context.getString(R.string.sell_owner) else item.shopName
//            )
            .getView<ImageView>(R.id.ivCateProduct)
        helper.getView<ImageView>(R.id.ivCateProduct2).visibility = if (item.shopCount == 0) {
            View.VISIBLE
        } else {
            View.GONE
        }
        ImageUtils.loadRoundCornerImage(context, imageView, item.image)


//        if (item.activePrice.isNullOrEmpty()) {
//            helper.setGone(R.id.tvCateProductPrice2, true)
//                .setGone(R.id.tvCateProductDiscount, true)
//                .setText(R.id.tvCateProductPrice, SpanUtil.setMoneySymbol(item.price, false))
//        } else {
//            helper.setGone(R.id.tvCateProductPrice2, false)
//                .setGone(R.id.tvCateProductDiscount, false)
//                .setText(R.id.tvCateProductPrice, SpanUtil.setMoneySymbol(item.activePrice, true))
//                .setText(R.id.tvCateProductPrice2, SpanUtil.setMoneySymbol(item.price, false))
//                .setText(R.id.tvCateProductDiscount, "${item.discount}折")
//            val tvCateProductPrice2 = helper.getView<TextView>(R.id.tvCateProductPrice2)
//            tvCateProductPrice2.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
//        }

        // 设置是显示市场价还是显示分享赚多少钱
//        with(helper.getView<TextView>(R.id.tvCateProductMarketPrice)) {
//            if (AccountManager.isMember() && !item.shareEarn.isNullOrEmpty() && item.shareEarn != "0") {
//                setTextColor(ContextCompat.getColor(context, R.color.colorMain))
//                paint.isAntiAlias = true
//                paint.flags = Paint.ANTI_ALIAS_FLAG
//                text = SpanUtil.setPrefixMoneyStr(price = item.shareEarn)
//            } else {
//                setTextColor(ContextCompat.getColor(context, R.color.colorLightText))
//                paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
//                paint.isAntiAlias = true
//                text = SpanUtil.setMoneySymbol(item.marketPrice)
//            }
//        }
//        // 判断是否显示标签
//        helper.setVisible(R.id.tvCateProductTagNotMember, !isMember)
//            .setGone(
//                R.id.tvCateProductTagMatter,
//                item.shareContent.isNullOrEmpty() && (item.shareImages == null || item.shareImages.isEmpty())
//            )
//            .setGone(
//                R.id.tvCateProductTagShareEarn,
//                !isMember || item.shareSwitch == null || item.shareSwitch != 1
//            )
    }
}